package com.realdolmen.whiteyellowcross.service;

import java.util.ArrayList;
import java.util.List;

import com.realdolmen.whiteyellowcross.datastore.WaitWorkShiftDayPlanDataStore;
import com.realdolmen.whiteyellowcross.memory.datastore.WaitMonthPlanningMemory;
import com.realdolmen.whiteyellowcross.memory.datastore.WaitWorkShiftPlanMemory;
import com.realdolmen.whiteyellowcross.model.WaitMonthPlanning;
import com.realdolmen.whiteyellowcross.model.WaitWorkShiftDayPlan;
import com.realdolmen.whiteyellowcross.util.DateTimeHelper;

public class PlanningPersistenceBasedService implements PlanningService {

    private WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore = null;

    private static PlanningService planningService;

    private PlanningPersistenceBasedService(
            WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore) {
        this.waitWorkShiftDayPlanDataStore = waitWorkShiftDayPlanDataStore;
    }

    public static PlanningService getInstance(
            WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore) {
        if (planningService == null) {
            synchronized (PersonelPersistenceBasedService.class) {

                if (planningService == null) {
                    planningService = new PlanningPersistenceBasedService(
                            waitWorkShiftDayPlanDataStore);

                }
            }
        }
        return planningService;
    }

    @Override
    public WaitMonthPlanning getWaitMonthPlanning(int year, int month) {

        WaitMonthPlanning waitMonthPlanning = new WaitMonthPlanning(new Long(year),
                new Long(month + 1));

        int daysInMonth = DateTimeHelper.getDaysInMonth(month, year);

        for (int i = 0; i < daysInMonth; i++) {

            List<WaitWorkShiftDayPlan> waitWorkShiftDayPlanningList = waitWorkShiftDayPlanDataStore.getWaitWorkShiftDayPlan(null, null);



            WaitWorkShiftDayPlan morningWaitWorkShiftPlan = WaitWorkShiftPlanMemory
                    .getRandomMorningWaitWorkShiftPlan();

            WaitWorkShiftDayPlan afternoonWaitWorkShiftPlan = WaitWorkShiftPlanMemory
                    .getRandomAfterNoonWaitWorkShiftPlan();
            WaitWorkShiftDayPlan eveningWaitWorkShiftPlan = WaitWorkShiftPlanMemory
                    .getRandomEveningWaitWorkShiftPlan();
            waitWorkShiftDayPlanningList.add(morningWaitWorkShiftPlan);
            waitWorkShiftDayPlanningList.add(afternoonWaitWorkShiftPlan);
            waitWorkShiftDayPlanningList.add(eveningWaitWorkShiftPlan);

            waitMonthPlanning.addWaitDayPlanning(i + 1, waitWorkShiftDayPlanningList);

        }

        //waitMonthPlannings.add(waitMonthPlanning);

        WaitMonthPlanning waitPlanning = WaitMonthPlanningMemory
                .getWaitPlanning(year, month);
        return waitPlanning;
    }

    public void updateWaitMonthPlanning(int year, int month, int day,
                                        String personelName, String shiftName) {

    }

    public void insertDefaultMonthPlanning(int year, int month) {

    }

}
